import sys                                             # import必要的包
from PyQt5.QtWidgets import QApplication, QComboBox, QVBoxLayout, QLabel, QWidget

class QComboBoxDemo(QWidget):
    def __init__(self, parent = None):
        super(QComboBoxDemo, self).__init__(parent)
        self.initUI()

    def initUI(self):
        self.setWindowTitle("combo box demo")
        self.resize(200,100)
        layout = QVBoxLayout()
        self.cb = QComboBox()
        self.label = QLabel('请选择串口')
        
        self.cb.addItem("com1")
        self.cb.addItem("com2")
        self.cb.addItems(["com3", "com4", "com5"])

        self.cb.currentIndexChanged.connect(self.selectionchange)
        layout.addWidget(self.cb)
        self.setLayout(layout)

    def selectionchange(self,i):
        print ("Items in the list are :\r\n")
        for count in range(self.cb.count()):
            print (self.cb.itemText(count))
        print ("Current index",i,"selection changed ",self.cb.currentText())
      

if __name__ == "__main__":
    app = QApplication(sys.argv)                        
    MainWindow = QComboBoxDemo()          #创建窗体对象                   
    MainWindow.show()                   #显示窗体对象
    sys.exit(app.exec_())